package data;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;


import dataconnect.ConnectionFactory;

public class DealPoint extends Thread{
	Map<String, BlockingQueue<Integer>> dealMap = new HashMap<String, BlockingQueue<Integer>>();
	Map<String, BlockingQueue<BigDecimal>> dealPriceMap = new HashMap<String, BlockingQueue<BigDecimal>>();
	Map<String, BlockingQueue<Integer>> dealBigQtyMap = new HashMap<String, BlockingQueue<Integer>>();
	Map<String,TimeStock> stockList = LoadData.getStockList();
	Map<String,Integer> avrQty = new HashMap<String,Integer>();
	
	public DealPoint() {
		Set<String> codeSet = stockList.keySet();
		for (Iterator<String> iterator = codeSet.iterator(); iterator.hasNext();) {
			String code = iterator.next();
			TimeStock ts = stockList.get(code);
			BlockingQueue<BigDecimal> priceBqueue = new ArrayBlockingQueue<BigDecimal>(15);
			dealPriceMap.put(code, priceBqueue);
			
			BlockingQueue<Integer> dealBigQty = new ArrayBlockingQueue<Integer>(15);
			dealBigQtyMap.put(code, dealBigQty);
			
			if(ts.getLastStock()==null||ts.getLastStock().getQty()==0){
				continue;
			}
			if(stockList.get(code).getTime()==null){
				try {
					System.out.println("wait 1 .."+code);
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				if(stockList.get(code).getTime()==null){
					try {
						System.out.println("wait 2 .."+code);
						Thread.sleep(3000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
			BlockingQueue<Integer> bqueue = new ArrayBlockingQueue<Integer>(16);
			dealMap.put(code, bqueue);
			bqueue.add(stockList.get(code).getQty());
			avrQty.put(code, stockList.get(code).getLastStock().getQty()/800);
			
		}
	}
	
	private void insertRecord(TimeStock ts,int bat) {
		String increStock = "insert dealpoint values('"+UUID.randomUUID()+"','"+ts.getCode()+"','"+ts.getStockName()+"','"+ts.getDate()+"','"+ts.getTime()+"',"+bat+")";
		try {
			Statement stmt = ConnectionFactory.getConnection().createStatement();
			stmt.execute(increStock);
			stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void run() {
		Set<String> codeSet = avrQty.keySet();
		while(true){
				for (String code : codeSet) {
					if(avrQty.get(code)==null||code.equals("sh000001")||code.equals("sz399001")){
						continue;
					}
					int avrqty = avrQty.get(code);
					BlockingQueue<Integer> bqueue = dealMap.get(code);
					BlockingQueue<BigDecimal> priceBqueue = dealPriceMap.get(code);
					BlockingQueue<Integer> dealBigQty = dealBigQtyMap.get(code);
					TimeStock ts = stockList.get(code);
					if(ts.getCurPrice()==null||ts.getPreDayPrice()==null){
						continue;
					}
					
					if(ts.getStockName().indexOf("*")>=0){
						continue;
					}
					
					try {
						if(bqueue.size()>=15){
							bqueue.poll();
						}
						bqueue.put(ts.getQty());
						
						if(dealBigQty.size()>=10){
							dealBigQty.poll();
						}
						
						if(priceBqueue.size()>=10){
							priceBqueue.poll();
						}
						priceBqueue.put(ts.getRatePercent());
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
					BigDecimal ratePercent = Constant.ZERO;
					BigDecimal[] rps = priceBqueue.toArray(new BigDecimal[priceBqueue.size()]);
					
					if(rps.length>1){
						for (int i = 1; i < rps.length; i++) {
							BigDecimal diffPercent = rps[i].subtract(rps[i-1]);
							ratePercent = ratePercent.add(diffPercent.abs());
						}
						ts.setPriceWive(ratePercent);
					}
					
					Object[] qtys = bqueue.toArray();
					
					if(qtys.length>1&&rps.length>1){
						int diffQty = (Integer)qtys[qtys.length-1]-(Integer)qtys[qtys.length-2];
						if((diffQty>39999&&ts.getLastStock().getAmount().compareTo(new BigDecimal("200000000"))<0)||ts.getCurPrice().multiply(new BigDecimal(diffQty)).compareTo(new BigDecimal(1000000))>0){
							try {
								if(rps[rps.length-1].compareTo(rps[rps.length-2])>0){
									dealBigQty.put((Integer)qtys[qtys.length-1]-(Integer)qtys[qtys.length-2]);
								}else{
									dealBigQty.put((Integer)qtys[qtys.length-2]-(Integer)qtys[qtys.length-1]);
								}
								Object[] bigQtys = dealBigQty.toArray();
								int countPositive = 0;
								int countNegtive = 0;
								for (int i = bigQtys.length-1; i>=0; i--) {
									int qty = (Integer)bigQtys[i];
									if(qty>0&&(Integer)bigQtys[bigQtys.length-1]>0){
										countPositive++;
										countNegtive=0;
									}else if(qty<0&&(Integer)bigQtys[bigQtys.length-1]<0){
										if(countNegtive>0){
											countPositive=0;
										}
										countNegtive++;
									}else{
										countPositive=0;
										countNegtive=0;
										break;
									}
									
								}
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
					
					if (qtys.length>3) {
						int qtys0 = (Integer)qtys[qtys.length-4];
						int qtys1 = (Integer)qtys[qtys.length-1];
						int qty18s = qtys1-qtys0;
						if(qty18s/avrqty>45){
							DataBuffer.addSeconds20Code(code);
							ts.setAmountInrecTime(ts.getTime());
							ts.setQtyBat(qty18s/avrqty);
						}
					}
					
					if (qtys.length>10) {
						int qtys0 = (Integer)qtys[qtys.length-11];
						int qtys3 = (Integer)qtys[qtys.length-1];
						int qtyOneMin = qtys3-qtys0;
						if(qtyOneMin/(avrqty*3)>18){
							DataBuffer.addOneMinCode(code);
//							insertRecord(ts,qtyOneMin/(avrqty*3));
							
							ts.setAmountInrecTime(ts.getTime());
							ts.setQtyBat(qtyOneMin/avrqty);
						}
					}
				}
				try {
					Thread.sleep(5996);//����ʱ��ռ4����
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
		}
		
	}
}
